GBML manual / Maths

dir_x

The x component of a direction/length pair - how far right (negative length: left) a step of that length in that direction reaches.

Syntax

dir_x(direction, length) -> real

Arguments

NameType
directionreal
lengthreal

Returns

real

Description

The horizontal component of a direction/length pair - how far right (negative length: left) a step of that length in that direction reaches. length * angle_cos(direction), already worked out.

Paired with dir_y, this turns a direction-and-speed into the hsp/vsp that actual movement understands - the exact shape a bounce or a launch needs.

Example

// Bounce off a horizontal paddle: flip the vertical half of the travel, keep the sideways
// half exactly as it was - the reflection a ball bouncing off a flat surface always needs.
// Assumes speed is left at 0 and hsp/vsp carry all of this object's own motion instead - the
// simplest shape to reason about, though not required: hsp's own note has the full story on
// how hsp/vsp and speed/direction can mix on the same instance.
direction = -direction;
hsp = dir_x(direction, 3);
vsp = dir_y(direction, 3);

Reflecting direction across the horizontal axis (-direction) keeps the sideways part of the travel and flips the up/down part - exactly what bouncing off a flat paddle or floor does to a ball's own path. dir_x/dir_y then turn that new direction straight into a fresh hsp/vsp kick.

No overflow to worry about here

Unlike dist_to/sqrt, there is no squaring anywhere in this one - angle_cos never exceeds a magnitude of 1, so the result never exceeds length itself, for any length real can hold.

On the GBA

length * angle_cos(direction) - never overflows on its own account, since sin/cos never exceed magnitude 1, so the result never exceeds length itself. Pairs with dir_y to turn a direction+speed into the hsp/vsp a bounce or a launch actually needs - exactly what reflecting a ball off a paddle needs.

See also: dir_ydir_toangle_cos